import {
  MigrationInterface,
  QueryRunner,
  Table,
  TableForeignKey,
  TableIndex,
} from 'typeorm';

export class CreateLikesAndCollectionsTables1730622000000
  implements MigrationInterface
{
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建点赞表
    await queryRunner.createTable(
      new Table({
        name: 'likes',
        columns: [
          {
            name: 'id',
            type: 'int',
            isPrimary: true,
            isGenerated: true,
            generationStrategy: 'increment',
          },
          {
            name: 'userId',
            type: 'int',
            comment: '用户ID',
          },
          {
            name: 'workId',
            type: 'int',
            comment: '作品ID',
          },
          {
            name: 'createdAt',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
            comment: '点赞时间',
          },
        ],
      }),
      true,
    );

    // 创建收藏表
    await queryRunner.createTable(
      new Table({
        name: 'collections',
        columns: [
          {
            name: 'id',
            type: 'int',
            isPrimary: true,
            isGenerated: true,
            generationStrategy: 'increment',
          },
          {
            name: 'userId',
            type: 'int',
            comment: '用户ID',
          },
          {
            name: 'workId',
            type: 'int',
            comment: '作品ID',
          },
          {
            name: 'createdAt',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
            comment: '收藏时间',
          },
        ],
      }),
      true,
    );

    // 为 likes 表添加外键
    await queryRunner.createForeignKey(
      'likes',
      new TableForeignKey({
        columnNames: ['userId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'users',
        onDelete: 'CASCADE',
      }),
    );

    await queryRunner.createForeignKey(
      'likes',
      new TableForeignKey({
        columnNames: ['workId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'works',
        onDelete: 'CASCADE',
      }),
    );

    // 为 collections 表添加外键
    await queryRunner.createForeignKey(
      'collections',
      new TableForeignKey({
        columnNames: ['userId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'users',
        onDelete: 'CASCADE',
      }),
    );

    await queryRunner.createForeignKey(
      'collections',
      new TableForeignKey({
        columnNames: ['workId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'works',
        onDelete: 'CASCADE',
      }),
    );

    // 创建唯一索引（防止重复点赞/收藏）
    await queryRunner.createIndex(
      'likes',
      new TableIndex({
        name: 'IDX_likes_userId_workId',
        columnNames: ['userId', 'workId'],
        isUnique: true,
      }),
    );

    await queryRunner.createIndex(
      'collections',
      new TableIndex({
        name: 'IDX_collections_userId_workId',
        columnNames: ['userId', 'workId'],
        isUnique: true,
      }),
    );

    // 创建查询索引
    await queryRunner.query(
      'CREATE INDEX `IDX_likes_workId` ON `likes` (`workId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_likes_userId` ON `likes` (`userId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_collections_workId` ON `collections` (`workId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_collections_userId` ON `collections` (`userId`)',
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // 删除索引
    await queryRunner.query(
      'DROP INDEX `IDX_collections_userId` ON `collections`',
    );
    await queryRunner.query(
      'DROP INDEX `IDX_collections_workId` ON `collections`',
    );
    await queryRunner.query('DROP INDEX `IDX_likes_userId` ON `likes`');
    await queryRunner.query('DROP INDEX `IDX_likes_workId` ON `likes`');

    await queryRunner.dropIndex('collections', 'IDX_collections_userId_workId');
    await queryRunner.dropIndex('likes', 'IDX_likes_userId_workId');

    // 删除表
    await queryRunner.dropTable('collections');
    await queryRunner.dropTable('likes');
  }
}
